import { _decorator, Component, EventTouch, Label, Node } from 'cc';
import { AllyUser } from '../entity/AllyUser';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { MemberOper } from './MemberOper';
import { EventManager } from '../event/EventManager';
import { Ally } from '../entity/Ally';
const { ccclass, property } = _decorator;

@ccclass('MemberItem')
export class MemberItem extends Component {
    
    @property(Label)
    rank:Label
    @property(Label)
    userName:Label
    @property(Label)
    job:Label
    @property(Label)
    point:Label
    @property(Label)
    people:Label
    @property(Label)
    villageNum:Label
    @property(Label)
    state:Label

    private user:AllyUser
    private ally:Ally

    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)

        EventManager.instance.on(EventManager.EventType.KickoutAlly, this.onKickoutAlly, this)
    }

    setData(user:AllyUser, ally:Ally){
        this.user = user
        this.ally = ally
        this.rank.string = user.Rank + ''
        this.userName.string = user.UserName
        this.job.string = user.AllyTitle
        this.people.string = user.People + ''
        this.villageNum.string = user.VillageNum + ''
    }

    async onTouchEnd(e:EventTouch){
      
        let ui = await UIManager.instance.openUI(PrefabEnum.MemberOper)
        ui.getComponent(MemberOper).setData(e, this.user, this.ally)
    }

    onKickoutAlly(u:AllyUser){
        if(u.UserId == this.user.UserId){
            this.node.destroy()
        }
    }
}


